/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package other;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Vector;
import model.Activity;
import model.Category;
import model.Cookbook;
import model.Ingredient;
import model.Product;
import model.Measure;
import model.Recipe;
import model.SearchList;
import org.xml.sax.InputSource;

/**
 *
 * @author Kamrat
 */
public class XMLUtils {

    private static Cookbook cookbook;
    public static String path = "test\\other\\nazwa2.xml";

    public static Cookbook readXml(String path) throws IOException {
        XStream xstream = new XStream(new DomDriver());
        xstream.alias("vector", Vector.class);
        xstream.alias("recipe", Recipe.class);
        xstream.alias("category", Category.class);
        xstream.alias("activity", Activity.class);
        xstream.alias("product", Product.class);
        xstream.alias("ingredient", Ingredient.class);
        xstream.alias("measure", Measure.class);

        InputStreamReader isr = new InputStreamReader(new FileInputStream(path), "UTF-8");
        Vector<Recipe> recipes = (Vector<Recipe>) xstream.fromXML(isr);

        Cookbook cookbook = new Cookbook();

        for (Recipe recipe : recipes) {
            Vector<Ingredient> ingredients = recipe.listIngredients();
            for (Ingredient ingredient : ingredients) {
                cookbook.addPossibleProduct(ingredient.getProduct());
            }
//            cookbook.addPossibleCategory(recipe.getCategory());
            cookbook.addRecipe(recipe);
        }
        cookbook.addPossibleMeasure(Measure.getGram());
        cookbook.addPossibleMeasure(Measure.getKilogram());
        cookbook.addPossibleMeasure(Measure.getLitre());
        cookbook.addPossibleMeasure(Measure.getMililitre());
        cookbook.addPossibleMeasure(Measure.getNoUnit());
        cookbook.addPossibleMeasure(Measure.getUnit());

        return cookbook;
    }

    public static boolean writeToXml(Cookbook cookbook) {
        XStream xstream = new XStream();
        xstream.alias("vector", Vector.class);
        xstream.alias("recipe", Recipe.class);
        xstream.alias("category", Category.class);
        xstream.alias("activity", Activity.class);
        xstream.alias("product", Product.class);
        xstream.alias("ingredient", Ingredient.class);
        xstream.alias("measure", Measure.class);
        try {
            Writer w = new OutputStreamWriter(new FileOutputStream(path), Charset.forName("UTF-8"));
            xstream.toXML(cookbook.listRecipesAsAVector(), w);
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    public static Cookbook getCookbook() {
        if (cookbook == null) {
            try {
                cookbook = XMLUtils.readXml(path);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
        return cookbook;
    }

    public static SearchList getRecipes() {
        try {

            return XMLUtils.readXml(path).list(true);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

    }
}
